package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.feng.entity.SysMenu;
import org.feng.entity.SysMenuRole;
import org.feng.entity.SysRole;
import org.feng.service.ISysMenuRoleService;
import org.feng.service.ISysMenuService;
import org.feng.condition.Condition;
import org.feng.annotation.AccessLog;
import org.feng.base.entity.Result;
import org.springframework.web.bind.annotation.*;
import org.feng.service.ISysRoleService;
import org.feng.base.controller.BaseController;

import jakarta.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 角色表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@RestController
@RequestMapping("/system/sysRole")
@Tag(name = "角色")
public class SysRoleController extends BaseController {

    @Resource
    private ISysRoleService sysRoleService;
    @Resource
    private ISysMenuRoleService sysMenuRoleService;
    @Resource
    private ISysMenuService sysMenuService;

    /**
     * 更新角色权限
     *
     * @param id 角色id
     * @return 返回体
     */
    @GetMapping("/updateRolePermission")
    @AccessLog("更新角色权限")
    @Operation(summary = "更新权限")
    public Result<Boolean> updateRolePermission(@RequestParam Long id, String menuIds) {
        sysMenuRoleService.remove(Wrappers.<SysMenuRole>lambdaQuery().eq(SysMenuRole::getRoleId, id));
        List<SysMenuRole> list = Arrays.stream(menuIds.split(",")).map(item -> {
            SysMenuRole sysMenuRole = new SysMenuRole();
            sysMenuRole.setRoleId(id);
            sysMenuRole.setMenuId(Long.valueOf(item));
            return sysMenuRole;
        }).collect(Collectors.toList());
        return Result.success(sysMenuRoleService.saveBatch(list));
    }

    /**
     * 获取角色下拥有的菜单和按钮
     *
     * @param id 角色id
     * @return 拥有的菜单id
     */
    @GetMapping("/rolePermissionTree")
    @Operation(summary = "获取角色下拥有的菜单按钮")
    public Result<List<String>> rolePermissionTree(@RequestParam Long id) {
        List<SysMenuRole> list = sysMenuRoleService.list(Wrappers.<SysMenuRole>lambdaQuery().eq(SysMenuRole::getRoleId, id));
        if (list.isEmpty()) {
            return Result.success(new ArrayList<>());
        }
        List<Long> menuIdList = list.stream().map(SysMenuRole::getMenuId).collect(Collectors.toList());
        List<SysMenu> menuList = sysMenuService.list(Wrappers.<SysMenu>lambdaQuery().in(SysMenu::getId, menuIdList));
        List<String> idList = menuList.stream().map(item -> String.valueOf(item.getId())).collect(Collectors.toList());
        return Result.success(idList);
    }

    /**
     * 分页列表
     *
     * @param sysRole 查询体
     * @return 返回体
     */
    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public Result<IPage<SysRole>> pageList(SysRole sysRole) {
        return Result.success(sysRoleService.page(Condition.getPage(), Wrappers.lambdaQuery(sysRole)));
    }

    /**
     * 列表
     *
     * @param sysRole 查询体
     * @return 返回体
     */
    @GetMapping("/list")
    @Operation(summary = "列表")
    public Result<List<SysRole>> list(SysRole sysRole) {
        return Result.success(sysRoleService.list(Wrappers.lambdaQuery(sysRole)));
    }

    /**
     * 保存
     *
     * @param sysRole 保存体
     * @return 返回体
     */
    @PostMapping("/save")
    @AccessLog("保存角色")
    @Operation(summary = "保存")
    public Result<Boolean> save(@RequestBody SysRole sysRole) {
        return Result.status(sysRoleService.save(sysRole));
    }

    /**
     * 更新
     *
     * @param sysRole 更新体
     * @return 返回体
     */
    @PostMapping("/update")
    @AccessLog("更新角色")
    @Operation(summary = "更新")
    public Result<Boolean> update(@RequestBody SysRole sysRole) {
        return Result.status(sysRoleService.updateById(sysRole));
    }

    /**
     * 删除
     *
     * @param ids 逗号分割的id
     * @return 返回体
     */
    @GetMapping("/remove")
    @AccessLog("删除角色")
    @Operation(summary = "删除")
    public Result<Boolean> remove(@RequestParam("ids") String ids) {
        return Result.status(sysRoleService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }
}
